Ext.define('Hi.paramadmin.view.VRule',{
    extend: 'Ext.grid.Panel',
    id: 'ruleGrid',
    title: 'Reglas',
    columns: [   
        {
            header: 'Nombre Regla',
            dataIndex: 'rulename'
        },
        {
            header: 'Descripcion',
            dataIndex: 'ruledesc'
        },
        {
            header: 'Activo/Inactivo',
            dataIndex: 'dinactive'
        }
    ],
    stripeRows: true,
    height: AppConfig.gridHeight,
    bbar: new Ext.PagingToolbar({  
        pageSize: AppConfig.gridPageSize,        
        store: Ext.data.StoreManager.lookup('rulest'),  
        displayInfo: true  
    }),
    tbar: [
        {
            text: 'Nueva Tarea',
            iconCls: 'add',
            handler: function(){
              
                var record = {
                    idrule: 0,
                    idsystem: '',
                    rulename: '',
                    ruledesc: '',
                    dinactive: ''
                }
                
                var form = Ext.create('Hi.paramadmin.view.CreateRule', {
                    id: 'ruleForm'
                });
                form.loadRecord(record);
                
                var w = Ext.create('Ext.window.Window',{
                    id: 'windowRule',
                    modal: true,                    
                    title: 'Crear Regla',
                    items: [
                        form
                    ]
                }).show();
            }
        },
        {
            text: 'Editar',
            iconCls: 'edit',
            handler: function(){                
                var grid = Ext.getCmp('ruleGrid');
                var rows = grid.getSelectionModel().getSelection();
                if(rows.length == 0){
                    return false;                    
                }
                var record = rows[0];
                
                var form = Ext.create('Hi.paramadmin.view.CreateRule',{
                    id: 'ruleForm'
                });
                form.loadRecord(record);
                
                var w = Ext.create('Ext.window.Window',{
                    id: 'windowRule',
                    modal: true,                    
                    title: 'Editar Regla',
                    items: [
                        form
                    ]
                }).show();                
            }
            
        },
        {
            text: 'Activar/Inactivar',
            iconCls: 'delete',
            handler: function(){
                
                var grid = Ext.getCmp('ruleGrid');
                var rows = grid.getSelectionModel().getSelection();
                
                if(rows.length == 0){
                    return false;
                }
                
                var record = rows[0];
                
                var changestate = (record.get('dinactive') == 'Inactivo')?'Activo':'Inactivo';               
                
                    Ext.Ajax.request({
                      url: 'paramadmin/Index/activeinactive',  
                      method: 'POST',
                      params: {
                            idrule: record.get('idrule')
                      },
                    success: function(response){
                        var obj = Ext.decode(response.responseText);
                        if(obj.success){
                            record.set('dinactive', changestate);                            
                            record.commit();
                        }
                    }
                });
                     
            }
        },
        {
            text: 'Atributos',
            iconCls: 'flag_blue',
            handler: function(){
                alert('Atributos');
            }
        }
    ]
    
});